package com.frt.sygang.sevice.impl;/**
 * Created by 冯瑞涛 on 2022/9/18 18:51
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frt.sygang.common.CustomException;
import com.frt.sygang.sevice.SetmealDishService;
import com.frt.sygang.sevice.SetmealService;
import com.frt.sygang.entity.dto.SetmealDto;
import com.frt.sygang.entity.Setmeal;
import com.frt.sygang.entity.SetmealDish;
import com.frt.sygang.mapper.SetmealMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 冯瑞涛
 * @date 2022/9/18 18:51
 * @version 1.0
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    @Transactional
    public void insSetmeakAndSetmaekDish(SetmealDto setmealDto) {
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        this.save(setmealDto);
        Long dtoId = setmealDto.getId();
        setmealDishes= setmealDishes.stream().map(temp -> {
            temp.setSetmealId(dtoId);
            return temp;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public SetmealDto getSetmealById(Long id) {
        Setmeal setmeal = this.getById(id);
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        SetmealDto setmealDto=new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        if(list!=null&&!list.isEmpty()){
            setmealDto.setSetmealDishes(list);
        }
        return setmealDto;
    }

    /**
     * 修改套餐业务
     * @param setmealDto
     */
    @Override
    public void setSetmeal(SetmealDto setmealDto) {
        //修改套餐信息
        this.updateById(setmealDto);
        Long id = setmealDto.getId();
        //删除原套餐包含的菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        setmealDishService.remove(queryWrapper);
        //添加修改后的菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(temp -> {
            temp.setSetmealId(id);
            return temp;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    @Transactional
    public void deleteSetmeal(Long[] ids) {
        List<Long> list= Arrays.asList(ids);
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,list);
        queryWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(queryWrapper);
        if(count>0){
            throw new CustomException("套餐处于在售状态，无法删除");
        }
        this.removeByIds(list);
        LambdaQueryWrapper<SetmealDish> wrapper=new LambdaQueryWrapper<>();
        wrapper.in(SetmealDish::getSetmealId,list);
        setmealDishService.remove(wrapper);
    }


}
